package com.kede.app.service.impl;

import com.kede.app.domain.ZhiImgs;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.ZhiImgsRepository;
import com.kede.app.service.ZhiImgsService;
import com.kede.app.service.dto.ZhiImgsDto;
import com.kede.app.service.dto.ZhiImgsQueryCriteria;
import com.kede.app.service.mapstruct.ZhiImgsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-07-02
**/
@Service
@RequiredArgsConstructor
public class ZhiImgsServiceImpl implements ZhiImgsService {

    private final ZhiImgsRepository zhiImgsRepository;
    private final ZhiImgsMapper zhiImgsMapper;

    @Override
    public Map<String,Object> queryAll(ZhiImgsQueryCriteria criteria, Pageable pageable){
        Page<ZhiImgs> page = zhiImgsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhiImgsMapper::toDto));
    }

    @Override
    public List<ZhiImgsDto> queryAll(ZhiImgsQueryCriteria criteria){
        return zhiImgsMapper.toDto(zhiImgsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhiImgsDto findById(Integer id) {
        ZhiImgs zhiImgs = zhiImgsRepository.findById(id).orElseGet(ZhiImgs::new);
        ValidationUtil.isNull(zhiImgs.getId(),"ZhiImgs","id",id);
        return zhiImgsMapper.toDto(zhiImgs);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhiImgsDto create(ZhiImgs resources) {
        return zhiImgsMapper.toDto(zhiImgsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhiImgs resources) {
        ZhiImgs zhiImgs = zhiImgsRepository.findById(resources.getId()).orElseGet(ZhiImgs::new);
        ValidationUtil.isNull( zhiImgs.getId(),"ZhiImgs","id",resources.getId());
        zhiImgs.copy(resources);
        zhiImgsRepository.save(zhiImgs);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            zhiImgsRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhiImgsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhiImgsDto zhiImgs : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("置换分类的末节点id", zhiImgs.getNodeId());
            map.put("图片要求多个用，分开", zhiImgs.getValues());
            map.put(" createTime",  zhiImgs.getCreateTime());
            map.put(" updateTime",  zhiImgs.getUpdateTime());
            map.put(" createBy",  zhiImgs.getCreateBy());
            map.put(" updateBy",  zhiImgs.getUpdateBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}